//
//  JYAddressController.m
//  JYFarm
//
//  Created by 花花 on 2017/10/12.
//  Copyright © 2017年 花花. All rights reserved.
//

#import "JYAddressController.h"
#import "JYAddressModel.h"
#import "JYAddressCell.h"
#import "AFNetworking.h"
#import "JYNetworkTool.h"
@interface JYAddressController ()

@property(nonatomic)NSArray * titleArr;

@property(nonatomic,copy) NSString *merchantName;//商家名称

@property(nonatomic,copy) NSString *receivingName; //收货人

@property(nonatomic,copy) NSString *contactnumber;//收货人电话

@property(nonatomic,copy) NSString *address; //收货地址

@property(nonatomic,copy) NSString *distributionTime; //配送时间
@end

static NSString *const JYAddressCellID = @"JYAddressCellID";

@implementation JYAddressController

#pragma mark - Life Cycle
- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self loadDataFromNetwork];
    [self initUI];
}
- (void)initUI{

    self.title = @"收货地址";
    
    [self.tableView registerClass:[JYAddressCell class] forCellReuseIdentifier:JYAddressCellID];
    UIView *header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, 33)];
    UILabel *labbel = [UILabel labelText:@"注：如需改动，请联系你的专属客服" fontSize:12 textColor:k9Color textAlignment:NSTextAlignmentLeft];
    labbel.frame =CGRectMake(10, 10, Screen_Width, 12);
    header.backgroundColor = kBgColor;
    [header addSubview:labbel];
    self.tableView.tableFooterView = header;
    self.tableView.rowHeight = kTableView_rowHeight;

}
#pragma mark - loadDataFromNetwork
- (void)loadDataFromNetwork{

    [JYNetworkTool POST:kUserReceivingAddressUrl parameter:@{@"userId":LH.userId} success:^(id resposeObject) {
       
        if (SUCCESS) {

            for (NSDictionary *dic in resposeObject[@"data"]) {
                
                JYAddressModel *model = [JYAddressModel mj_objectWithKeyValues:dic];
                
                self.merchantName = model.merchantName;
                self.receivingName = model.receivingName;
                self.contactnumber = model.contactnumber;
                self.address = model.address;
                self.distributionTime = model.distributionTime;
            }
            
            [self.tableView reloadData];
        }else{
            [MBProgressHUD LY_ShowError:resposeObject[@"msg"] time:2.0];
        }
    } failure:^(NSError *error) {
        [MBProgressHUD LY_ShowError:kNoNetworkTips time:2.0];
    }];

}
#pragma mark - <UITableViewDataSource,UITableViewDelegate>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.titleArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    JYAddressCell *cell = [tableView dequeueReusableCellWithIdentifier:JYAddressCellID];
    
    cell.titleLabel.text = [self.titleArr safeObjectAtIndex:indexPath.row];
    
    
    switch (indexPath.row) {
        case JYAddressType_MerchantName:
            cell.subLabel.text = self.merchantName;
            break;
        case JYAddressType_ReceivingName:
            cell.subLabel.text = self.receivingName;
            break;
        case JYAddressType_Contactnumber:
            cell.subLabel.text =  self.contactnumber;
            break;
        case JYAddressType_Address:
            cell.subLabel.text =   self.address;
            break;
        case JYAddressType_DistributionTime:
            cell.subLabel.text = self.distributionTime;
            break;
        default:
            break;
    }
    return cell;

}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}

#pragma mark - Setter && Getter Methods
- (NSArray *)titleArr{

    if (!_titleArr) {
        _titleArr = @[@"商家名称：",@"收货人：",@"收货人电话：",@"收货地址：",@"配送时间："];
    }

    return _titleArr;
}



@end
